class Solution {
public:
    vector<vector<int>> permuteUnique(vector<int>& nums) {
        sort(nums.begin(), nums.end());
        vector<bool> used(nums.size(), false);
        dfs(nums, used, 0);
        return res;
    }

private:
    vector<int> path;
    vector<vector<int>> res;

    void dfs(vector<int>& nums, vector<bool> used, int depth) {
        if (depth == nums.size()) {
            res.push_back(path);
            return;
        }
        for (int i = 0; i < nums.size(); ++i) {
            // 剪枝
            if (used[i] || (i > 0 && nums[i] == nums[i - 1] && !used[i - 1])) {
                continue;
            }
            path.push_back(nums[i]);
            used[i] = true;
            dfs(nums, used, depth + 1);
            used[i] = false;
            path.pop_back();
        }
    }
};